package com.example.taobaounion3.ui.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.example.taobaounion3.model.domain.HomePagerContent;
import com.example.taobaounion3.utils.urlUtils;

import java.util.ArrayList;
import java.util.List;

public class LooperPagerAdapt extends PagerAdapter {

    private List<HomePagerContent.DataBean> mData=new ArrayList<>();
    private OnlooperPageItemClickListener mItemClickListener=null;

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        //处理一下越界问题
        int realPosition=position%mData.size();
        HomePagerContent.DataBean dataBean=mData.get(realPosition);
        String coverUrl= urlUtils.getCoverPath(dataBean.getPict_url());
        ImageView iv=new ImageView(container.getContext());
        ViewGroup.LayoutParams layoutParams=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
        iv.setLayoutParams(layoutParams);
        iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
        Glide.with(container.getContext()).load(coverUrl).into(iv);
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mItemClickListener!=null) {
                    HomePagerContent.DataBean item=mData.get(realPosition);
                    mItemClickListener.onLooperItemClick(item);
                }
            }
        });
        container.addView(iv);
        return iv;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }

    @Override
    public int getCount() {

        return Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view==object;
    }

    public void setData(List<HomePagerContent.DataBean> contents) {
        mData.clear();
        mData.addAll(contents);
        notifyDataSetChanged();
    }

    public int getDataSize(){
        return mData.size();
    }

    public void setOnlooperPageItemClickListener(OnlooperPageItemClickListener listener){
        this.mItemClickListener=listener;
    }

    public interface OnlooperPageItemClickListener{
        void onLooperItemClick(HomePagerContent.DataBean item);
    }
}
